package com.etc.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Music;
import com.etc.service.MusicService;
import com.etc.utils.FTPUtil;
import com.etc.utils.FileUtil;
import com.etc.utils.IDUtils;
import com.github.pagehelper.PageInfo;

@Controller
public class AdminMusicController {

	@Autowired
	private MusicService musicService;

	/**
	 * 上传文件
	 * 
	 * @param files
	 *            文件对象集合
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@RequestMapping("/uploadMusic")
	@ResponseBody
	public String pload(Music music, @RequestParam MultipartFile[] files) throws IllegalStateException, IOException {
		List<File> fileList = new ArrayList<File>();
		for (MultipartFile multipartFile : files) {
			if (!multipartFile.isEmpty()) {
				String originalFilename = multipartFile.getOriginalFilename();
				String extentName = originalFilename.substring(originalFilename.lastIndexOf("."));
				String name = IDUtils.genImageName() + extentName;
				if (extentName.equals(".jpg")) {
					music.setMimg(name);
				} else if (extentName.equals(".txt")) {
					music.setMdesc(name);
				} else {
					music.setMurl(name);
				}
				FileUtil.makeDir("D:/upload");
				File file = new File("D:/upload/" + name);
				multipartFile.transferTo(file);
				fileList.add(file);
			}
		}
		String message = null;
		boolean b = FTPUtil.uploadFile(fileList);
		if (b) {
			music.setMcreatetime(new Date());
			musicService.addMusic(music);
		} else {
			message = "上传失败";
			return message;
		}
		message = "上传成功";
		return message;
	}

	@RequestMapping("admin_music_info")
	public ModelAndView musicList(
			@RequestParam(value = "pageSize", required = true, defaultValue = "6") Integer pageSize,
			@RequestParam(value = "pageNum", required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(value = "mstatus", required = true, defaultValue = "all") String mstatus) {

		PageInfo<Music> pageInfo = musicService.pageMusic(pageSize, pageNum, mstatus);
		ModelAndView modelAndView = new ModelAndView("admin_music_info");
		modelAndView.addObject("musiclist", pageInfo.getList());
		modelAndView.addObject("mstatus", mstatus);
		modelAndView.addObject("page", pageInfo);
		modelAndView.addObject("info", "");

		return modelAndView;
	}
	
	
	/**
	 * 模糊查询
	 * @param mstatus
	 * @param info
	 * @return
	 */
	@RequestMapping("admin_music_like")
	public ModelAndView selectByNameOrTitle(
			@RequestParam(value = "pageSize", required = true, defaultValue = "6") Integer pageSize,
			@RequestParam(value = "pageNum", required = true, defaultValue = "1") Integer pageNum,
			String mstatus,String info){
		ModelAndView modelAndView = new ModelAndView("admin_music_info");
		PageInfo<Music> pageInfo=musicService.selectByNameOrTitle(pageSize,pageNum,mstatus,info);
		modelAndView.addObject("musiclist", pageInfo.getList());
		modelAndView.addObject("mstatus", mstatus);
		modelAndView.addObject("page", pageInfo);
		modelAndView.addObject("info", info);
		return modelAndView;
	}
	
	/**
	 * 通过ID删除
	 * @param mid
	 */
	@RequestMapping("deletemusic")
	@ResponseBody
	public String  deletemusic(Integer mid){
		
		String message = musicService.deletemusic(mid);
		return message;
	}
	
	
	
	/**
	 * 改变歌曲状态
	 * @param mAid
	 * @param mid
	 * @return
	 */
	@RequestMapping("updatemAid")
	@ResponseBody
	public String updatemAid(Integer mAid,Integer mid){
		musicService.updatemAid(mAid,mid);
		return "修改成功";
	}
	
}
